package java_class_work.week07.jdbc;

import java_class_work.week07.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * @Auther: admin
 * @Date: 2022/10/25 10:51
 * @Description:
 */

public class Update {
    public static void main(String[] args) {
        Map<String, String> in = input();
        Connection con = JDBCUtils.getConnection();
        String sql = "update user set password = ? where username = ?";
        PreparedStatement ps = null;
        try {
            ps = con.prepareStatement(sql);
            ps.setString(1,in.get("pwd"));
            ps.setString(2,in.get("name"));
            int i = ps.executeUpdate();
            System.out.println("effect rows"+i);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            JDBCUtils.close();
        }
    }



    private static Map<String,String> input(){
        Map<String, String> hm = new HashMap<>();
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入你的账号：");
        hm.put("name",scanner.nextLine());
        System.out.print("请输入新密码：");
        hm.put("pwd",scanner.nextLine());
        return hm;
    }
}
